/*
 * $Id: NiceProperties.java,v 1.7 2003/05/21 22:56:16 znamenacek Exp $
 * 
 * Copyright (c) Tomas Znamenacek
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

package net.sf.vofce.util;

import java.util.Properties;
import java.util.Iterator;
import java.util.Vector;
import java.util.List;
import java.awt.Color;

/**
 * <code>Properties</code> extension providing some new features.
 * <ul>
 * 	<li>Easier manipulation with native types (<code>double</code>, ...).</li>
 * 	<li>Property change notificiation.</li>
 * </ul>
 * 
 * @author Tomas Znamenacek
 */
public class NiceProperties extends Properties {

	List listeners = new Vector();

	//
	// Listener agenda
	//

	public void addChangeListener(PropertyChangeListener l) {

		listeners.add(l);
	}

	public void removeChangeListener(PropertyChangeListener l) {

		listeners.remove(l);
	}

	public void firePropertyChanged(String key) {

		Iterator i = listeners.iterator();

		while (i.hasNext()) {

			((PropertyChangeListener) i.next()).propertyChanged(key);
		}
	}

	//
	// String
	// 

	public void setString(String key, String value) {

		setProperty(key, value);
		firePropertyChanged(key);
	}

	public String getString(String key) {

		return getProperty(key);
	}

	public String getString(String key, String defaultValue) {

		return getProperty(key, defaultValue);
	}
	
	//
	// int
	//
	
	/** Associates int value with given key. */
	public void setInt(String key, int value) {

		setProperty(key, Integer.toString(value));
		firePropertyChanged(key);
	}
	
	/** Returns int value associated with given key. */
	public int getInt(String key) {

		return Integer.parseInt(getProperty(key));
	}
	
	/** Returns int value associated with given key. */
	public int getInt(String key, int defaultValue) {
		
		String foo = getProperty(key, "");
		return (foo.length() == 0) ? defaultValue : Integer.parseInt(foo);
	}

	//
	// float
	//
	
	/** Associates float value with given key. */
	public void setFloat(String key, float value) {

		setProperty(key, Float.toString(value));
		firePropertyChanged(key);
	}
	
	/** Returns float value associated with given key. */
	public float getFloat(String key) {
		
		return Float.parseFloat(getProperty(key));
	}
	
	/** Returns float value associated with given key. */
	public float getFloat(String key, float defaultValue) {
		
		String foo = getProperty(key, "");
		return (foo.length() == 0) ? defaultValue : Float.parseFloat(foo);
	}

	//
	// double
	//
	
	/** Associates double value with given key. */
	public void setDouble(String key, double value) {

		setProperty(key, Double.toString(value));
		firePropertyChanged(key);
	}
	
	/** Returns double value associated with given key. */
	public double getDouble(String key) {
		
		return Double.parseDouble(getProperty(key));
	}
	
	/** Returns double value associated with given key. */
	public double getDouble(String key, double defaultValue) {
		
		String foo = getProperty(key, "");
		return (foo.length() == 0) ? defaultValue : Double.parseDouble(foo);
	}

	//
	// boolean
	//
	
	/** Associates boolean value with given key. */
	public void setBoolean(String key, boolean value) {

		setProperty(key, Boolean.toString(value));
		firePropertyChanged(key);
	}
	
	/** Returns boolean value associated with given key. */
	public boolean getBoolean(String key) {
		
		return Boolean.getBoolean(getProperty(key));
	}
	
	/** Returns boolean value associated with given key. */
	public boolean getBoolean(String key, boolean defaultValue) {
		
		String foo = getProperty(key, "");
		return (foo.length() == 0) ? defaultValue : Boolean.getBoolean(foo);
	}

	//
	// Color
	//
	
	/** Associates Color value with given key. */
	public void setColor(String key, Color value) {

		setProperty(key+".r", "" + value.getRed());
		setProperty(key+".g", "" + value.getGreen());
		setProperty(key+".b", "" + value.getBlue());
		firePropertyChanged(key);
	}
	
	/** Returns Color value associated with given key. */
	public Color getColor(String key) {
		
		int r = Integer.parseInt(getProperty(key+".r"));
		int g = Integer.parseInt(getProperty(key+".g"));
		int b = Integer.parseInt(getProperty(key+".b"));

		return new Color(r, g, b);
	}
	
	/** Returns Color value associated with given key. */
	public Color getColor(String key, Color defaultValue) {
	
		int r = Integer.parseInt(getProperty(key+".r", "" + defaultValue.getRed()));
		int g = Integer.parseInt(getProperty(key+".g", "" + defaultValue.getGreen()));
		int b = Integer.parseInt(getProperty(key+".b", "" + defaultValue.getBlue()));
		
		return new Color(r, g, b);
	}
}
